package extension

import android.view.View
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

//@Deprecated("使用core-ktx库中的")
//fun View?.visible() {
//  this?.visibility = View.VISIBLE
//}
//
//fun View?.visible(visible: Boolean) {
//  this?.visibility = if (visible) View.VISIBLE else View.GONE
//}
//
//fun View?.gone() {
//  this?.visibility = View.GONE
//}
//
//fun View?.isVisible(): Boolean {
//  return this?.visibility == View.VISIBLE
//}

// 取代View.post{}
// suspendCoroutine 和 suspendCancellableCoroutine 可以将回调方法转为协程
// suspendCancellableCoroutine 可以取消，而suspendCoroutine不可以
suspend fun View.await() = suspendCoroutine<View> { coroutine ->
    this.post {
        coroutine.resume(this)
//        coroutine.resumeWithException()
    }
}
